
package com.be.beadmin.product.impl;

import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.basicData.dto.output.UserEx;
import com.be.beadmin.basicData.service.UserService;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import com.be.beadmin.product.dto.input.WrokingHoursOverViewDto;
import com.be.beadmin.product.dto.output.SumTimingAndCurStartTime;
import com.be.beadmin.product.dto.output.WrokingHoursOverViewEx;
import com.be.beadmin.product.entity.WrokingHoursOverView;
import com.be.beadmin.product.entity.WrokingHoursOverView_;
import com.be.beadmin.product.mapper.WrokingHoursOverViewMapper;
import com.be.beadmin.product.mapper.WrokingHoursOverViewMapperEx;
import com.be.beadmin.product.repository.WrokingHoursOverViewRepository;
import com.be.beadmin.product.service.WrokingHoursOverViewService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @description 服务实现
* @author zjm
* @date 2023-04-24
**/
@Service
@RequiredArgsConstructor
public class WrokingHoursOverViewServiceImpl implements WrokingHoursOverViewService {

    @Autowired
    private  WrokingHoursOverViewRepository wrokingHoursOverViewRepository;

    @Autowired
    private UserService userService;
    @Autowired
    private  WrokingHoursOverViewMapper wrokingHoursOverViewMapper;
    @Autowired
    private  WrokingHoursOverViewMapperEx wrokingHoursOverViewExMapper;

    @Override
    public Page<WrokingHoursOverViewEx> getWrokingHoursOverViewByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<WrokingHoursOverView> page = wrokingHoursOverViewRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilder.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(WrokingHoursOverView_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(WrokingHoursOverView_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<WrokingHoursOverViewEx> page1 = page.map(wrokingHoursOverViewExMapper::toEntity);
            return page1;

    }

    @Override
    public void addWrokingHoursOverView(WrokingHoursOverViewDto wrokingHoursOverViewDto) {
            WrokingHoursOverView result = wrokingHoursOverViewRepository.findWrokingHoursOverViewByIdAndDelete(wrokingHoursOverViewDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("WrokingHoursOverView不存在,请重新查询");
            }
            WrokingHoursOverView wrokingHoursOverView = wrokingHoursOverViewMapper.toEntity(wrokingHoursOverViewDto);
            wrokingHoursOverViewRepository.save(wrokingHoursOverView);
    }

    @Override
    public void updateWrokingHoursOverView(WrokingHoursOverViewDto wrokingHoursOverViewDto) {
            WrokingHoursOverView wrokingHoursOverView = wrokingHoursOverViewRepository.findWrokingHoursOverViewById(wrokingHoursOverViewDto.getId());
            wrokingHoursOverView = wrokingHoursOverViewMapper.toEntity(wrokingHoursOverViewDto);
            wrokingHoursOverViewRepository.save(wrokingHoursOverView);
    }


    @Override
    public void delWrokingHoursOverView(String id) {
        WrokingHoursOverView wrokingHoursOverView = wrokingHoursOverViewRepository.findWrokingHoursOverViewById(id);
        wrokingHoursOverView.setDelete(1);
        wrokingHoursOverViewRepository.save(wrokingHoursOverView);
    }

    @Override
    public void delWrokingHoursOverViews (List<String> ids) { }

    @Override
    public SumTimingAndCurStartTime getSumTimingAndCurStartTime(HttpServletRequest request, String processId) {
        UserEx userEx = userService.viewBaseInfo(request);
        WrokingHoursOverView hoursOverView = wrokingHoursOverViewRepository.findWrokingHoursOverViewByProcessIdAndUserIdAndDelete(processId,userEx.getId(), 0);
        if (hoursOverView==null) {
            hoursOverView = new WrokingHoursOverView();
            hoursOverView.setSumWorkHours((float) 0);
            hoursOverView.setCurrStartTime(null);
            hoursOverView.setAccWorkHours((float) 0);
            hoursOverView.setProcessId(processId);
        }

        SumTimingAndCurStartTime sumTimingAndCurStartTime = new SumTimingAndCurStartTime();
        sumTimingAndCurStartTime.setCurStartTime(hoursOverView.getCurrStartTime());
        sumTimingAndCurStartTime.setSumTiming(hoursOverView.getSumWorkHours()==null ? 0 : (float)hoursOverView.getSumWorkHours());

        return sumTimingAndCurStartTime;
    }
}